package com.qf.fmall2302.wxpay;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpRequest;
import org.apache.shiro.crypto.hash.Md5Hash;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

public class WxPayUtils {

    public static final String appid = "wx632c8f211f8122c6";
    public static final String mch_id = "1497984412";
    public static final String appkey = "sbNCm1JnevqI36LrEaxFwcaT0hkGxFnC";

    public static final String unifiedorder_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    public static final String orderquery_url = "https://api.mch.weixin.qq.com/pay/orderquery";


    public static String getPayUrl(String orderid,String desc,Long amount){
        //1. 组织请求map，必传字段如下
        HashMap<String, String> map = new HashMap<>();
        map.put("appid",appid);
        map.put("mch_id",mch_id); //商户号
        String randomstr = UUID.randomUUID().toString().replaceAll("-", "");
        map.put("nonce_str",randomstr); // 长度32个字符的随机字符串，干扰项
        map.put("body",desc); // 订单描述信息

        map.put("out_trade_no",orderid); // 订单编号
        // 使用 amount 替换下面的金额
        map.put("total_fee","1");  // 1 分钱
        map.put("trade_type","NATIVE"); // 支付类型
        map.put("fee_type","CNY"); // 币种
        map.put("notify_url","http://47.118.45.73:8080/pay/callback"); // 商户提供的回调地址
        map.put("sign_type","MD5"); // 签名算法

        // 签名处理
        //1. 对所有参数按照参数名称进行升序排序
        TreeMap<String, String> treeMap = new TreeMap<>(map);

        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, String> entry : treeMap.entrySet()) {
            builder.append(entry.toString());
            builder.append("&");
        }
        String stringA = builder.toString();

        String tempString = stringA + "key=" + appkey;

        String sign = new Md5Hash(tempString).toHex().toUpperCase();
        map.put("sign",sign);

        // map --> xml 字符串
        String xml = XmlUtil.mapToXmlStr(map);

        String result = HttpRequest.post(unifiedorder_url)
                .body(xml)
                .execute()
                .body();

        // xml str ----> map
        Map<String, Object> resultMap = XmlUtil.xmlToMap(result);
        String url = (String) resultMap.get("code_url");

        return url;

    }

    public static String queryStatus(String orderid) {
//1. 组织请求map，必传字段如下
        HashMap<String, String> map = new HashMap<>();
        map.put("appid",appid);
        map.put("mch_id",mch_id); //商户号
        String randomstr = UUID.randomUUID().toString().replaceAll("-", "");
        map.put("nonce_str",randomstr); // 长度32个字符的随机字符串，干扰项

        map.put("out_trade_no",orderid); // 订单编号
        map.put("sign_type","MD5"); // 签名算法

        // 签名处理
        //1. 对所有参数按照参数名称进行升序排序
        TreeMap<String, String> treeMap = new TreeMap<>(map);

        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, String> entry : treeMap.entrySet()) {
            builder.append(entry.toString());
            builder.append("&");
        }
        String stringA = builder.toString();

        String tempString = stringA + "key=" + appkey;

        String sign = new Md5Hash(tempString).toHex().toUpperCase();
        map.put("sign",sign);

        // map --> xml 字符串
        String xml = XmlUtil.mapToXmlStr(map);

        String result = HttpRequest.post(orderquery_url)
                .body(xml)
                .execute()
                .body();

        // xml str ----> map
        Map<String, Object> resultMap = XmlUtil.xmlToMap(result);
        System.out.println(resultMap);
        return (String) resultMap.get("trade_state");

    }



    public static void main(String[] args) {

//        String payUrl = getPayUrl("202308251056098", "test", 1l);
//        System.out.println(payUrl);

        queryStatus("202308251056098");
    }
}
